#include "rasterFormats.hpp"
#include "readRasterKeys.hpp"
#include "streamAccess.hpp"

namespace hms::raster
{


Raster readRaster(std::istream& is, Format format, const fs::path& formatFile){
	using F = Format;
	switch(format){
		case F::esri:
			return readRaster_esri(is);
			break;
		case F::custom:
			return readRaster_custom(is, formatFile);
		default:
			throw std::logic_error("Not implemented!");
	}
}

Raster readRaster(
	const fs::path& path,
	Format format,
	const fs::path& formatFile
){
	std::ifstream file { hms::getReadFile( path, false ) };
	return readRaster(file, format, formatFile);
}

Raster readRaster_esri( std::istream& is ){
	RasterReader reader {{
		/* ncols in the raster are the number of cells in x direction,
		 * therefore they correspond to nx */
		"ncols",
		"nrows",
		"xllcorner",
		"yllcorner",
		"xllcenter",
		"yllcenter",
		"cellsize",
		"nodata_value"
	}};
	reader.reverseY = true;

	return reader.read(is);
}

Raster readRaster_custom( std::istream& is, const fs::path& formatFile ){
	RasterReader reader { readRasterKeys(formatFile) };

	return reader.read(is);
}

} // namespace hms::raster
